package com.sky.service;

import com.sky.entity.AdminActionLog;
import com.sky.repository.AdminActionLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminLogService {
    @Autowired
    private AdminActionLogRepository logRepository;

    @Async("logExecutor")
    public void saveLogAsync(AdminActionLog log) {
        logRepository.save(log);
    }

    public List<AdminActionLog> getRecentLogs() {
        // 查询最近10条日志，按创建时间降序排列
        int pageSize = 10;
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(0, pageSize, sort);
        return logRepository.findAll(pageRequest).getContent();
    }
}